package com.iemes.controller.entryform;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;     
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.BrandFormMap;
import com.iemes.entity.BrandModelFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.EntryFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.QualityFromMap;
import com.iemes.entity.RoleFormMap;
import com.iemes.entity.SuppFilleFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.entity.PurchaseFromMap;
import com.iemes.mapper.DepartmentMapper;
import com.iemes.mapper.ResourcesMapper;
import com.iemes.mapper.SuppfilleMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.entry.EntryMapper;
import com.iemes.mapper.material.PurchaseMapper;
import com.iemes.mapper.material.QualityMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;


@RequestMapping("/entry/")
@Controller
public class EntryfromController extends BaseController {
	@Inject
	private SuppfilleMapper suppMapper;
	@Inject
	private PurchaseMapper purMapper;
	@Inject
	private QualityMapper qualityMapper;
	@Inject
	private EntryMapper entryMapper;
	
	@RequestMapping("examinepurde")
	public String examineprus(Model model){
		List<EntryFormMap> entryFromMap=entryMapper.findByWhere(new EntryFormMap());
		model.addAttribute("entry", entryFromMap);
		List<PurchaseFromMap> purFormMaps=purMapper.findByWhere(new PurchaseFromMap());
		model.addAttribute("pur", purFormMaps);
		List<QualityFromMap> qualityFormMap=qualityMapper.findByWhere(new QualityFromMap());
		model.addAttribute("qualitya", qualityFormMap);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/entryform/entryform_list";
	}

	@ResponseBody
	@RequestMapping("entry_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		EntryFormMap entryFromMap = getFormMap(EntryFormMap.class);
		List<Map<String, Object>> entryFormMaps=entryMapper.getAllName(entryFromMap);
		LuogicPaging(pageNow, pageSize, entryFormMaps);
		pageView.setRecords(entryFormMaps);
		return pageView;
	}
	
	//编辑订单
		@RequestMapping("entery_edit")
		public String editUI(Model model) throws Exception 
		{
			String id = getPara("id");
			if(Common.isNotEmpty(id))
			{
				EntryFormMap entFormMaps = entryMapper.findbyFrist("id", id, EntryFormMap.class);
				model.addAttribute("before", entFormMaps);
			}

			return Common.BACKGROUND_PATH + "/entryform/entryform_edit";
		}

	
	
	@ResponseBody
	@RequestMapping(value="entery_editEntity")
	@SystemLog(module="物料管理",methods="采购单-修改表")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		EntryFormMap entryFormMap=getFormMap(EntryFormMap.class);
		baseMapper.editEntity(entryFormMap);
		return "success";
	}

	@RequestMapping("entery_addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/entryform/entryform_add";
	}
	
	
	
	@ResponseBody
	@RequestMapping("entery_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="进仓单-新增物料进仓清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		EntryFormMap enteryFromMap = getFormMap(EntryFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		baseMapper.addEntity(enteryFromMap);
		// 物料清单子项
		return "success";
	}
	
	
	//假删除
	@ResponseBody
	@RequestMapping("entery_delEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="物料管理",methods="出仓订单-删除出仓清单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		EntryFormMap enteryFormMap=getFormMap(EntryFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for(int i=0;i<str.length;i++){
			enteryFormMap.put("date", sdf.format(date));
			enteryFormMap.set("status", 0);
			enteryFormMap.put("id", str[i]);
			entryMapper.editEntity(enteryFormMap);
		}
		return "success";
	}
	
	
	
}

